package com.julian.delegatePattern.gumballMachine.server;


import java.rmi.RemoteException;
import java.util.Random;

public class HasQuarterState implements State {
    Random randomWinner = new Random(System.currentTimeMillis());//随机数
    transient GumballMachine gumballMachine;

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("you can't insert another quarter");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("Quarter returned");
        gumballMachine.setState(gumballMachine.noQuarterState);
    }

    @Override
    public void turnCrank() {
        System.out.println("you turned ...");
        int winner = randomWinner.nextInt(10);
        try {
            if((winner == 0) && (gumballMachine.getCount() >= 2)){
                gumballMachine.setState(gumballMachine.getWinnerState());
            }else {
                gumballMachine.setState(gumballMachine.getSoldState());
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void dispense() {
        System.out.println("No gumball dispensed");
    }
}
